package org.mark.storage.service.impl;

import org.mark.common.response.Result;
import org.mark.storage.entity.StorageInfo;
import org.mark.storage.mapper.StorageInfoMapper;
import org.mark.storage.service.StorageInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class StorageInfoServiceImpl implements StorageInfoService {

    @Resource
    private StorageInfoMapper storageInfoMapper;

    @Override
    public Result page(Integer pageNum, Integer pageSize, String name) {
        LambdaQueryWrapper<StorageInfo> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(name)) {
            wrapper.like(StorageInfo::getName, name)
                    .or()
                    .like(StorageInfo::getAddress, name);
        }
        wrapper.orderByDesc(StorageInfo::getCreateTime);

        Page<StorageInfo> page = storageInfoMapper.selectPage(
                new Page<>(pageNum, pageSize),
                wrapper
        );
        return Result.success(page);
    }

    @Override
    public Result save(StorageInfo storageInfo) {
        // 参数校验
        if (StringUtils.isEmpty(storageInfo.getAddress())) {
            return Result.fail("储位位置不能为空");
        }
        if (storageInfo.getCapacity() == null || storageInfo.getCapacity() < 0) {
            return Result.fail("容量必须大于等于0");
        }
        if (StringUtils.isEmpty(storageInfo.getStatus())) {
            return Result.fail("请选择储位类型");
        }

        storageInfo.setCreateTime(new Date());
        storageInfo.setUpdateTime(new Date());

        storageInfoMapper.insert(storageInfo);
        return Result.success("添加成功");
    }

    @Override
    public Result update(StorageInfo storageInfo) {
        if (storageInfo.getId() == null) {
            return Result.fail("ID不能为空");
        }
        if (StringUtils.isEmpty(storageInfo.getAddress())) {
            return Result.fail("储位位置不能为空");
        }

        storageInfo.setUpdateTime(new Date());
        storageInfoMapper.updateById(storageInfo);
        return Result.success("更新成功");
    }

    @Override
    public Result remove(Integer id) {
        if (id == null) {
            return Result.fail("ID不能为空");
        }

        // TODO: 检查是否有生鲜在使用该储位

        storageInfoMapper.deleteById(id);
        return Result.success("删除成功");
    }

    @Override
    public Result list() {
        List<StorageInfo> list = storageInfoMapper.selectList(
                new LambdaQueryWrapper<StorageInfo>()
                        .orderByAsc(StorageInfo::getName)
        );
        return Result.success(list);
    }

    @Override
    public Result getStorageInventory() {
        List<Map<String, Object>> storageList = storageInfoMapper.selectStorageInventory();
        return Result.success(storageList);
    }
}